/*
 * Copyright (C) 2011 Mette Bank, Rikke Jensen, Kenneth Brodersen, Thomas Pedersen
 * 
 * This file is part of digiPECS.
 * 
 * digiPECS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * digiPECS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with digiPECS.  If not, see <http://www.gnu.org/licenses/>.
 */

package sw6.digipecs.helpers;

import java.io.File;
import java.io.IOException;

import sw6.digipecs.R;
import android.content.Context;
import android.media.MediaRecorder;
import android.widget.Toast;

public class SoundRecorder {
	private Context mContext;
	private static SoundRecorder mInstance;
	private MediaRecorder mMediaRecorder; 
	private String mPath; 
	protected SoundRecorder(Context c)
	{
		mContext = c; 
	}
	public static SoundRecorder getinstance(Context context)
	{
		if(mInstance!=null)
			return mInstance; 
		else 
		{
			mInstance = new SoundRecorder(context); 
			return mInstance; 
		}
	}
	public void startRecording(int imgId)
	{
		try {
			File file = DigiLoader.getFile(mContext, DigiLoader.PECS_DIR_PATH, imgId + ".3gp");
			
			mPath = file.getPath(); 
			mMediaRecorder=new MediaRecorder(); 
			mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
			mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
			mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
			
			mMediaRecorder.setOutputFile(mPath);

			mMediaRecorder.prepare();
			mMediaRecorder.start(); 
			Toast.makeText(mContext, "start recording", 1000).show(); 
		} catch (IOException e) {
			ToastHelper.showError(mContext, R.string.errorSoundNotSavedIO);
			e.printStackTrace();
		}

		
	}
	public String stopRecording()
	{
		mMediaRecorder.stop(); 
		mMediaRecorder.release(); 
		Toast.makeText(mContext, "stop recording", 1000).show(); 
		return mPath; 
	}
}
